//
//  thingCell.m
//  UI_HLJ_Frame
//
//  Created by dlios on 15-7-8.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//
#import "MJRefresh.h"
#import "thingCell.h"
#import "MyCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "weddingThingModel.h"
#import "thingdetailsViewController.h"
@implementation thingCell


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];


            self.heightArr=[[NSMutableArray array]init];
        //创建 collectionVeiw
        self.collectionView_GG = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height-64-49) collectionViewLayout:flowLayout];
        self.collectionView_GG.delegate = self;
        self.collectionView_GG.dataSource = self;
        self.collectionView_GG.backgroundColor=[UIColor whiteColor];
        [self.collectionView_GG registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
        [self.collectionView_GG addFooterWithTarget:self action:@selector(footerRereshing)];
        
        [self.contentView addSubview:self.collectionView_GG];
    }
    return self;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier:
                    @"MyCell" forIndexPath:indexPath];
    myCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        myCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
        
    }];

    NSInteger remainder=indexPath.row%2;
    NSInteger currentRow=indexPath.row/2;
    

    CGFloat   currentHeight=[self.heightArr[indexPath.row] floatValue];
    myCell.hightimage = currentHeight;
    CGFloat positonX=(self.contentView.frame.size.width/2-10)*remainder+10*(remainder+1);
    CGFloat positionY=(currentRow+1)*10;
    for (NSInteger i=0; i<currentRow; i++) {
        NSInteger position=remainder+i*2;
        positionY+=[self.heightArr[position] floatValue];
    }
    myCell.frame = CGRectMake(positonX, positionY,(self.contentView.frame.size.width/2-20),currentHeight) ;
//判断是第几个collectionview  并赋值
    switch (self.flag) {
        case 0:
        {       weddingThingModel  *thing = self.hunshaarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//                [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
                myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;
        };
        case 1:{
            weddingThingModel  *thing = self.lifuarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
        case 2:{
            weddingThingModel  *thing = self.hunxiearr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
        case 3:{
            weddingThingModel  *thing = self.peishiarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
        case 4:{
            weddingThingModel  *thing = self.shoushiarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
        case 5:{
            weddingThingModel  *thing = self.hunqingarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
        default: {
            weddingThingModel  *thing = self.jujiaarr[indexPath.row];
               [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path] placeholderImage:[UIImage imageNamed:@"占位图"]];
//            [myCell.myImageView sd_setImageWithURL:[NSURL URLWithString:thing.photo_path ]];
            myCell.myLabel.text= [NSString stringWithFormat:@"￥%@",thing.price];
            return  myCell;
            break;  };
    }
  

    
}

//
//实现返回对应内容的高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (self.flag) {
        case 0:
        {   weddingThingModel  *thing = self.hunshaarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
            [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;
        };
        case 1:{
            weddingThingModel  *thing = self.lifuarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
        case 2:{
            weddingThingModel  *thing = self.hunxiearr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
        case 3:{
            weddingThingModel  *thing = self.peishiarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
        case 4:{
            weddingThingModel  *thing = self.shoushiarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
        case 5:{
            weddingThingModel  *thing = self.hunqingarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
        default: {
            weddingThingModel  *thing = self.jujiaarr[indexPath.row];
            CGFloat a=[thing.height floatValue];
            CGFloat b =[thing.width floatValue];
            CGFloat height = (a/b*self.contentView.frame.size.width/2)+30;
                [self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
            return  CGSizeMake((self.contentView.frame.size.width/2)-20, height);
            break;  };
    }
   }
//各个collectview 的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    switch (self.flag) {
        case 0:
        {  return self.hunshaarr.count;
         
            break;
        };
        case 1:
        {  return self.lifuarr.count;
            
            break;
        };
        case 2:
        {  return self.hunxiearr.count;
            
            break;
        };
        case 3:
        {  return self.peishiarr.count;
            
            break;
        };
        case 4:
        {  return self.shoushiarr.count;
            
            break;
        };
        case 5:
        {  return self.hunqingarr.count;
            
            break;
        };
        default:
        {  return self.jujiaarr.count;
            
            break;
        };
    }}
//初始化数组
- (void)setHunshaarr:(NSMutableArray *)hunshaarr{
    
    if (_hunshaarr != hunshaarr) {
        [_hunshaarr release];
        _hunshaarr = [hunshaarr retain];
    }
    [self.collectionView_GG reloadData];
    
}

- (void)setLifuarr:(NSMutableArray *)lifuarr{
    
    if (_lifuarr != lifuarr) {
        [_lifuarr release];
        _lifuarr = [lifuarr retain];
    }
    [self.collectionView_GG reloadData];
    
}

- (void)setHunxiearr:(NSMutableArray *)hunxiearr{
    
    if (_hunxiearr != hunxiearr) {
        [_hunxiearr release];
        _hunxiearr = [hunxiearr retain];
    }
    [self.collectionView_GG reloadData];
    
}

- (void)setPeishiarr:(NSMutableArray *)peishiarr{
    
    if (_peishiarr != peishiarr) {
        [_peishiarr release];
        _peishiarr = [peishiarr retain];
    }
    [self.collectionView_GG reloadData];
    
}
- (void)setShoushiarr:(NSMutableArray *)shoushiarr{
    
    if (_shoushiarr != shoushiarr) {
        [_shoushiarr release];
        _shoushiarr = [shoushiarr retain];
    }
    [self.collectionView_GG reloadData];
    
}
//下啦加载

- (void)footerRereshing
{ static NSInteger a=2;
    switch (self.flag) {
        case 0:
        {  NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=1&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.hunshaarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
        
    }];
            
};
            break;
           case 1:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=2&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.lifuarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];};
            case 2:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=3&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.hunxiearr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];};
            case 3:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=4&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.peishiarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];};
        case 4:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=5&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.shoushiarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];};
        case 5:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=6&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.hunqingarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];};
        
        default:
        {NSString *url = [NSString stringWithFormat:@"http://www.hunliji.com/products.json?category=7&sort=0&page=%ld",a];
            AFHTTPRequestOperationManager *homecellmanager = [AFHTTPRequestOperationManager manager];
            [homecellmanager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSMutableDictionary *dic = responseObject;
                NSMutableArray *arr = dic[@"products"];
                for (NSDictionary *tempdic in arr) {
                    weddingThingModel *thing= [[weddingThingModel alloc]init];
                    [thing setValuesForKeysWithDictionary:tempdic];
                    [self.jujiaarr addObject:thing];
                    [thing release];
                }
                [self.collectionView_GG reloadData];
                [self.collectionView_GG footerEndRefreshing];
                a++;
            }
             
                         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                             
                             
                         }];
        };
            break;
    }
    

}
//通过协议 实现点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    weddingThingModel *thing= [[weddingThingModel alloc]init];
    if (self.flag==0) {
        thing = self.hunshaarr[indexPath.row];
        [self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];

    }else if(self.flag==1){
      
        thing = self.lifuarr[indexPath.row];
      [self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];
 

       
    }else if(self.flag==2){
   
        thing = self.hunxiearr[indexPath.row];
[self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];


    }else if(self.flag==3){
  
        thing = self.peishiarr[indexPath.row];
    [self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];


    }else if(self.flag==4){
  
        thing = self.shoushiarr[indexPath.row];
[self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];


    }else if(self.flag==5){
    
        thing = self.hunqingarr[indexPath.row];
[self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];


    }else {

        thing = self.jujiaarr[indexPath.row];
[self.delegatey collectionView:self.collectionView_GG didSelectItemAtIndexPath:indexPath model:thing];

    }

    
}
- (void)setHunqingarr:(NSMutableArray *)hunqingarr{
    
    if (_hunqingarr != hunqingarr) {
        [_hunqingarr release];
        _hunqingarr = [hunqingarr retain];
    }
    [self.collectionView_GG reloadData];
    
}
- (void)setJujiaarr:(NSMutableArray *)jujiaarr{
    
    if (_jujiaarr != jujiaarr) {
        [_jujiaarr release];
        _jujiaarr = [jujiaarr retain];
    }
    [self.collectionView_GG reloadData];
    
}



@end
